package com.west_traveler.tiyutoutiao.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.west_traveler.tiyutoutiao.R;
import com.west_traveler.tiyutoutiao.fragment.main_fragment.GameFrg;
import com.west_traveler.tiyutoutiao.fragment.main_fragment.HeadLineFrg;
import com.west_traveler.tiyutoutiao.fragment.main_fragment.NewsFrg;

import java.util.LinkedList;

/**
* @author Wang_JunJie
* @date 2016/12/15
*
*/
public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private RadioGroup radioGroup;
    private NewsFrg newsFrg;
    private HeadLineFrg headLineFrg;
    private GameFrg gameFrg;
    private LinkedList<Fragment> frgs;
    private FragmentManager supportFragmentManager;
    private Fragment currentFragment;
    private RelativeLayout rlMainToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initview();
        //关于RadioGroup
        aboutRadioGroup();

    }
    //关于RadioGroup
    private void aboutRadioGroup() {
        //界面启动 默认选中第一个
        RadioButton radioButton = (RadioButton) radioGroup.getChildAt(0);
        radioButton.setChecked(true);
        radioButton.setTextColor(0xffff0000);
        currentFragment = frgs.get(0);
        supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        transaction.add(R.id.main_container_id,currentFragment);
        transaction.commit();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                    if (radioButton.isChecked()) {
                        radioButton.setTextColor(0xffff0000);
                        //修改占位的容器控件
                        changeContainerWidget(i);
                    }else{
                        radioButton.setTextColor(0xff000000);
                    }
                }

            }
        });
    }
    //修改占位的容器控件
    private void changeContainerWidget(int position) {

        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.hide(currentFragment);
        Fragment nowFragment = frgs.get(position);
        if (!nowFragment.isAdded()) {
            fragmentTransaction.add(R.id.main_container_id,nowFragment);
        }
        fragmentTransaction.show(nowFragment);
        currentFragment=nowFragment;
        fragmentTransaction.commit();

    }

    //初始化控件
    private void initview() {
        toolbar = ((Toolbar) findViewById(R.id.toolbar_main_id));
        radioGroup = ((RadioGroup) findViewById(R.id.rg_main_id));

        frgs = new LinkedList<>();
        newsFrg = new NewsFrg();
        headLineFrg = new HeadLineFrg();
        gameFrg = new GameFrg();
        frgs.add(newsFrg);
        frgs.add(headLineFrg);
        frgs.add(gameFrg);
    }
}
